<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>章鱼世界</title>
  <meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover">

  <meta name="apple-mobile-web-app-capable" content="yes" />
  <!-- 隐藏状态栏/设置状态栏颜色：只有在开启WebApp全屏模式时才生效。content的值为default | black | black-translucent 。 -->
  <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" /> <!-- 添加到主屏后的标题 -->
  <meta name="apple-mobile-web-app-title" content="标题"> <!-- 忽略数字自动识别为电话号码 -->
  <meta content="telephone=no" name="format-detection" /> <!-- 忽略识别邮箱 -->
  <meta content="email=no" name="format-detection" />
  <meta name="apple-itunes-app" content="app-id=myAppStoreID, affiliate-data=myAffiliateData, app-argument=myURL" />
  <!-- 添加智能 App 广告条 Smart App Banner：告诉浏览器这个网站对应的app，
    并在页面上显示下载
    banner:https://developer.apple.com/library/ios/documentation/AppleApplications/Reference/SafariWebContent/PromotingAppswithAppBanners/PromotingAppswithAppBanners.html -->
  <!-- 针对手持设备优化，主要是针对一些老的不识别viewport的浏览器，比如黑莓 -->
  <meta name="HandheldFriendly" content="true"> <!-- 微软的老式浏览器 -->
  <meta name="MobileOptimized" content="320"> <!-- uc强制竖屏 -->
  <meta name="screen-orientation" content="portrait"> <!-- QQ强制竖屏 -->
  <meta name="x5-orientation" content="portrait"> <!-- UC强制全屏 -->
  <meta name="full-screen" content="yes"> <!-- QQ强制全屏 -->
  <meta name="x5-fullscreen" content="true"> <!-- UC应用模式 -->
  <meta name="browsermode" content="application"> <!-- QQ应用模式 -->
  <meta name="x5-page-mode" content="app"> <!-- windows phone 点击无高光 -->
  <meta name="msapplication-tap-highlight" content="no">
  <link rel="stylesheet" href="https://at.alicdn.com/t/font_929739_9hiq23wl1mf.css">
  <script src="http://yx-web.nos.netease.com/official/websdk/NIM_Web_SDK_v4.8.0.js"></script>

  <script>
    function GetApiHref() {
      //获取接口域名
      return 'https://api.912cc.com/'
    }
    window.Judge = false;
    var channel = null;
    if (location.protocol == 'file:') {
      window.Judge = true
    }
    var wgtVer = null;
    document.addEventListener('plusready', function () {
      console.log('plusready is running')
      // 获取本地应用资源版本号
      plus.runtime.getProperty(plus.runtime.appid, function (inf) {
        wgtVer = inf.version.split(".").join("")
      });




      // 获取支付通道
      plus.payment.getChannels(function (channels) {
        channel = channels
        console.log('channels')
        console.log(JSON.stringify(channels))

      }, function (e) {
        alert("获取支付通道失败：" + e.message);
      });

      backButton();
      getNewVersion();
    }, false);

    // 判断返回键方法
    function backButton() {

      plus.key.addEventListener('backbutton', function () {
        if ('Android' == plus.os.name) {
          if (location.hash != '#/') {
            window.history.back();
          } else {
            plus.nativeUI.confirm('确认退出？', function (e) {
              if (e.index > 0) {
                plus.runtime.quit();
              }
            }, '', ['取消', '确定']);
          }
        } else {
          if (location.hash != '#/') {
            window.history.back();
          } else {
            if (confirm('确认退出？')) {
              if (e.index > 0) {
                plus.runtime.quit();
              }
            }
          }
        }
      }, false);
    }

    // 获取最新版本号方法
    function getNewVersion() {
      //str1 本地版本号
      //str2 最新版本号
      var checkURL = GetApiHref() + 'JYEPay/GetJYEVersion'
      console.log(checkURL)
      plus.nativeUI.showWaiting('检测更新...');
      var xhr = new XMLHttpRequest(); // 定义ajax请求
      xhr.onreadystatechange = function () {
        switch (xhr.readyState) {
          case 4:
            plus.nativeUI.closeWaiting();
            console.log(xhr.status)
            if (xhr.status == 200) {
              console.log('检测更新成功：' + xhr.responseText);
              var newVer = xhr.responseText.split('.').join("");
              console.info(wgtVer + '<' + newVer);

              if (wgtVer && newVer && (wgtVer < newVer)) {
                if ('Android' == plus.os.name) {
                  plus.nativeUI.confirm('检测到有新的版本,是否更新？', function (e) {
                    if (e.index > 0) {
                      downLoad(); // 下载升级包
                    } else {
                      plus.runtime.quit();
                    }
                  }, '', ['取消', '确定']);

                } else {
                  if (confirm('检测到有新的版本,是否更新？')) {

                    downLoad(); // 下载升级包

                  }
                }
              } else {
                plus.nativeUI.closeWaiting();
              }
            } else {
              plus.nativeUI.closeWaiting();
              plus.nativeUI.showWaiting('检测更新失败！');
            }
            break;
          default:
            break;
        }
      }
      xhr.open('GET', checkURL);
      xhr.setRequestHeader(
        'Content-Type', 'application/x-www-form-urlencoded'
      );
      xhr.withCredentials = true;

      xhr.send();
    }

    // 下载最新包方法
    var downloadURL;

    function downLoad() {
      plus.nativeUI.showWaiting('正在下载...');

      console.info(downloadURL)
      if ('Android' == plus.os.name) {
        downloadURL = "http://download.912cc.com/gamedownfile/912cc.apk"
        if (window.Judge) {
          plus.nativeUI.closeWaiting();
          plus.nativeUI.showWaiting('正在下载...');
          var task = plus.downloader.createDownload(downloadURL, {
            filename: '_doc/update/'
          }, function (d, status) {
            console.log(status)
            if (status == 200) {
              console.log('下载成功：' + d.filename);
              install(d.filename); // 安装wgt包
            } else {
              console.log('下载失败！');
              plus.nativeUI.alert('更新失败！');
              plus.nativeUI.closeWaiting();
            }
          });


          task.addEventListener("statechanged", function (download, status) {
            switch (download.state) {
              case 2:
                break;
              case 3:
                var percent = download.downloadedSize / download.totalSize * 100;
                plus.nativeUI.closeWaiting();
                plus.nativeUI.showWaiting("已下载 " + parseInt(percent) + "%");
                break;
              case 4:
                plus.nativeUI.closeWaiting();
                plus.nativeUI.showWaiting("下载完成");
                break;
            }
          });

          task.start();
        }
      } else {
        downloadURL = "itms-services://?action=download-manifest&url=https://www.912cc.com/iosInhouse/manifest.plist"
        console.log(downloadURL);
        plus.nativeUI.closeWaiting();
        plus.nativeUI.showWaiting('正在安装...');
        location.href = downloadURL
      }
    }

    function install(file) {
      plus.runtime.install(file);
    }

  </script>
  <!-- <link href=./static/css/app.906f9f9107fe0a11ac8d7dafc2b00c12.css rel=stylesheet> -->
</head>

<body>
  <div id="app"></div>
  <!-- built files will be auto injected -->
</body>

</html>
<style>
  html,
  body,
  #app {
    height: 100%;
    /* width: 100%; */
    font-family: 'PingFang-SC-Medium';

  }

  * {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
    outline: none;
  }

  *:not(input, textarea) {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
  }

</style>
